package com.ktg.ktgcommon.domain.user;


import com.fasterxml.jackson.annotation.JsonIgnore;

import com.ktg.ktgcommon.query.BaseQuery;
import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

@Data
public class SysUser extends BaseQuery implements UserDetails, Serializable {

  private Integer userId;
  private String userName;
  private String nickName;
  private String userType;
  private String email;
  private String phonenumber;
  private String sex;
  private String avatar;
  private String password;
  private String status;
  private String delFlag;
  private String loginIp;
  private java.sql.Timestamp loginDate;
  private String createBy;
  private java.sql.Timestamp createTime;
  private String updateBy;
  private java.sql.Timestamp updateTime;
  private String remark;
  private Integer roleId;


  public int getUserId() {
    return userId;
  }

  public void setUserId(Integer userId) {
    this.userId = userId;
  }


  public String getNickName() {
    return nickName;
  }

  public void setNickName(String nickName) {
    this.nickName = nickName;
  }


  public String getUserType() {
    return userType;
  }

  public void setUserType(String userType) {
    this.userType = userType;
  }


  public String getEmail() {
    return email;
  }

  public void setEmail(String email) {
    this.email = email;
  }


  public String getPhonenumber() {
    return phonenumber;
  }

  public void setPhonenumber(String phonenumber) {
    this.phonenumber = phonenumber;
  }


  public String getSex() {
    return sex;
  }

  public void setSex(String sex) {
    this.sex = sex;
  }


  public String getAvatar() {
    return avatar;
  }

  public void setAvatar(String avatar) {
    this.avatar = avatar;
  }


  public String getPassword() {
    return password;
  }

  @Override
  public String getUsername() {
    return this.userName;
  }

  public void setUsername(String username){
    this.userName = username;
  }

  @Override
  @JsonIgnore
  public boolean isAccountNonExpired() {
    return this.roleId == 1;
  }

  @Override
  @JsonIgnore
  public boolean isAccountNonLocked() {
    return this.roleId == 1;
  }

  @Override
  @JsonIgnore
  public boolean isCredentialsNonExpired() {
    return this.roleId == 1;
  }

  @Override
  @JsonIgnore
  public boolean isEnabled() {
    return this.roleId == 1;
  }
  /**
   * 用户的角色列表
   */
  private List<String> roleList;

  /**
   * 用户的权限列表
   */
  private List<String> premissionList;


  public void setPassword(String password) {
    this.password = password;
  }


  public String getStatus() {
    return status;
  }

  public void setStatus(String status) {
    this.status = status;
  }
  

  public List<String> getRoleList() {
    return roleList;
  }

  public void setRoleList(List<String> roleList) {
    this.roleList = roleList;
  }

  public List<String> getPremissionList() {
    return premissionList;
  }

  public void setPremissionList(List<String> premissionList) {
    this.premissionList = premissionList;
  }

  public String getDelFlag() {
    return delFlag;
  }

  public void setDelFlag(String delFlag) {
    this.delFlag = delFlag;
  }


  public String getLoginIp() {
    return loginIp;
  }

  public void setLoginIp(String loginIp) {
    this.loginIp = loginIp;
  }


  public java.sql.Timestamp getLoginDate() {
    return loginDate;
  }

  public void setLoginDate(java.sql.Timestamp loginDate) {
    this.loginDate = loginDate;
  }


  public String getCreateBy() {
    return createBy;
  }

  public void setCreateBy(String createBy) {
    this.createBy = createBy;
  }


  public java.sql.Timestamp getCreateTime() {
    return createTime;
  }

  public void setCreateTime(java.sql.Timestamp createTime) {
    this.createTime = createTime;
  }


  public String getUpdateBy() {
    return updateBy;
  }

  public void setUpdateBy(String updateBy) {
    this.updateBy = updateBy;
  }


  public java.sql.Timestamp getUpdateTime() {
    return updateTime;
  }

  public void setUpdateTime(java.sql.Timestamp updateTime) {
    this.updateTime = updateTime;
  }


  public String getRemark() {
    return remark;
  }

  public void setRemark(String remark) {
    this.remark = remark;
  }

  @JsonIgnore
  @Override
  public Collection<? extends GrantedAuthority> getAuthorities() {
    List<GrantedAuthority> list = new ArrayList<>();
    if (this.getRoleList()!= null) {
      this.getRoleList().forEach(role -> list.add(new SimpleGrantedAuthority(role)));
    }
    if (this.getPremissionList()!=null){
      this.getPremissionList().forEach(premission -> list.add(new SimpleGrantedAuthority(premission)));
    }
    return list;
  }

}
